import Base from './BaseElement';
import Paint from '../Paint';
import CanvasUtils from '@/utils/CanvasUtils';
import { ArcI, SpotI, StyleI } from '@/utils/CanvasUtils/CanvasType';

export interface RoundI extends ArcI, StyleI { }

export default class RoundElement extends Base {

	public round: RoundI;

	private save: ArcI | null = null;

	public constructor(paint: Paint, round: RoundI) {
		super(paint)
		this.round = round;
	}

	public Draw(canvas: CanvasUtils): void {
		const { Spot: { X, Y }, Radius, StartAngle, EndAngle, Counterclockwise, BackgroundColor, BorderColor, LineWidth } = this.round;
		const save = this.save = { Spot: { X, Y }, Radius, StartAngle, EndAngle, Counterclockwise };
		canvas.Save();
		canvas.BeginPath();
		canvas.Arc(save);
		canvas.ClosePath();
		if (BackgroundColor) {
			canvas.FillStyle = BackgroundColor;
			canvas.Fill();
		}
		if (BorderColor) {
			canvas.StrokeStyle = BorderColor;
			if (LineWidth) {
				canvas.LineWidth = LineWidth;
			}
			canvas.Stroke();
		}
		canvas.Restore();
	}

	public OnRange({ X: x, Y: y }: SpotI): boolean {
		if (!this.save) {
			return false;
		}
		const { Spot: { X, Y }, Radius } = this.save;
		if (Radius === 0) return false;
		const dx = X - x;
		const dy = Y - y;
		return dx * dx + dy * dy <= Radius * Radius;
	}

}
